local hx__lvheng = fk.CreateSkill{
  name = "hx__lvheng",
}

hx__lvheng:addEffect("active", {
  name = "hx__lvheng",
  anim_type = "drawcard",
  prompt = "#hx__lvheng",
  card_num = 1,
  target_num = 0,
  times = function(self, player)
    return player.phase == Player.Play and 2 + player:getMark("@hx__lvheng-turn") - player:usedEffectTimes(self.name, Player.HistoryPhase) or -1
  end,
  can_use = function(self, player)
    return player:usedEffectTimes(self.name, Player.HistoryPhase) < 2 + player:getMark("@hx__lvheng-turn") and not player:isNude()
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local type = Fk:getCardById(effect.cards[1]).type
    local cards = {}
    local x
    if player:getMark("hx__lvheng_count-turn") > 0 then
      x = player:getMark("hx__lvheng_count-turn") + 1
    else
      x = 1
    end
    room:recastCard(effect.cards, player, hx__lvheng.name,{"@@hx__lvheng-turn", x})
    if player.dead then return end
    local card1 = table.filter(room.draw_pile, function(id)
      return Fk:getCardById(id).type ~= type
    end)
    if #card1 > 0 then
      local card = Fk:getCardById(table.random(card1))
      table.insert(cards, card)
      local card2 = table.filter(room.draw_pile, function(id)
        return Fk:getCardById(id).type ~= type and Fk:getCardById(id).type ~= card.type
      end)
      if #card2 > 0 then
        table.insert(cards, Fk:getCardById(table.random(card2)))
      else
        local card_2dis = table.filter(room.discard_pile, function(id)
          return Fk:getCardById(id).type ~= type and Fk:getCardById(id).type ~= card.type
        end)
        if #card_2dis > 0 then
          table.insert(cards, Fk:getCardById(table.random(card_2dis)))
        end
      end
    else
      local card1_dis = table.filter(room.discard_pile, function(id)
        return Fk:getCardById(id).type ~= type
      end)
      if #card1_dis > 0 then
        local card = Fk:getCardById(table.random(card1_dis))
        table.insert(cards, card)
        local card2_dis = table.filter(room.discard_pile, function(id)
          return Fk:getCardById(id).type ~= type and Fk:getCardById(id).type ~= card.type
        end)
        if #card2_dis > 0 then
          table.insert(cards, Fk:getCardById(table.random(card2_dis)))
        else
          local card_2dis = table.filter(room.draw_pile, function(id)
            return Fk:getCardById(id).type ~= type and Fk:getCardById(id).type ~= card.type
          end)
          if #card_2dis > 0 then
            table.insert(cards, Fk:getCardById(table.random(card_2dis)))
          end
        end
      end
    end
    room:obtainCard(player, cards, false, fk.ReasonPrey, player, hx__lvheng.name,{"@@hx__lvheng-turn", x})
    room:addPlayerMark(player, "hx__lvheng_count-turn", 2)
  end,
})


hx__lvheng:addEffect(fk.AfterCardsMove, {
  anim_type = "special",
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__lvheng.name) and player:getMark("hx__lvheng_count-turn") > 0 then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId):getMark("@@hx__lvheng-turn") > 0 then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId):getMark("@@hx__lvheng-turn") > 0 then
            local x = Fk:getCardById(info.cardId):getMark("@@hx__lvheng-turn")
            local y = table.filter(player:getCardIds("h"), function(id)
              return Fk:getCardById(id):getMark("@@hx__lvheng-turn") == x
            end)
            room:setCardMark(Fk:getCardById(info.cardId), "@@hx__lvheng-turn", 0)
            local mark
            if player:getTableMark("hx__lvheng_@@mark-turn") == nil then
              mark = {}
            else
              mark = player:getTableMark("hx__lvheng_@@mark-turn")
            end
            if #y == 0 and not table.contains(mark, x) then
              player:broadcastSkillInvoke(hx__lvheng.name)
              room:notifySkillInvoked(player, hx__lvheng.name)
              room:addPlayerMark(player, "@hx__lvheng-turn", 1)
              table.insertIfNeed(mark, x)
              room:setPlayerMark(player, "hx__lvheng_@@mark-turn", mark)
            end
          end 
        end
      end
    end
  end,
  })

return hx__lvheng